﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.Projections;

namespace GisControl.MapProviders.BaiduMapProvider
{
    public class BaiduStarMapProvider : BaiduMapProviderBase
    {
        public static readonly BaiduStarMapProvider Instance;
        
        static readonly string UrlFormat = "http://shangetu{0}.map.bdimg.com/it/u=x={1};y={2};z={3};v={4};type={5}&fm={6}&udt=20140929";

        readonly Guid id = new Guid("EA7925B1-29A0-45A8-BD3C-5CC96C2ACCA4");

        public override Guid Id
        {
            get { return id; }
        }

        public string CnName
        {
            get
            {
                return "百度卫星地图";
            }
        }
        
        public override string Name
        {
            get
            {
                return "BaiduSatelliteMap";
            }
        }

        public string Type
        {
            get
            {
                return "sate";
            }
        }

        public string Version
        {
            get
            {
                return "009";
            }
        }

        public string fm
        {
            get
            {
                return "46";
            }
        }

        static BaiduStarMapProvider()
        {
            Instance = new BaiduStarMapProvider();
        }

        public override PureImage GetTileImage(GPoint pos, int zoom)
        {
            try
            {
                string url = MakeTileImageUrl(pos, zoom, LanguageStr);
                return GetTileImageUsingHttp(url);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        string MakeTileImageUrl(GPoint pos, int zoom, string language)
        {
            var num = (pos.X + pos.Y) % 4 + 1;
            string url = string.Format(UrlFormat, num, pos.X, pos.Y, zoom);
            return url;
        }
    }
}
